<?php
/* SVN FILE: $Id: players_controller.php 250 2010-06-17 10:55:41Z gregory.rome $ */
/**
 * Players Controller.
 *
 * Players teams.
 *
 * cBBweb : Digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 * @version       $Revision: 250 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-06-17 10:55:41 +0000 (Thu, 17 Jun 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Players Controller.
 *
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 */
class PlayersController extends AppController {

	var $name = 'Players';
	var $helpers = array('Html', 'Form');
	/*var	$paginate = array(
 		'Player' => array(
 			'limit' => 32,
			'contain' => array(
				'Team' => array (
					'fields' => array()
				)
			)
		)
	);*/

	function index() {
		$this->Player->recursive = 2;
        $players = $this->paginate('Player');
        if (isset($this->params['requested'])) {
            return $players;
        } else {
            $this->set('players', $players);
        }
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Player.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Player->recursive = 2;
		$player = $this->Player->read(null, $id);
        if (isset($this->params['requested'])) {
            return $player;
        } else {
            $this->set('player', $player);
        }
	}

	function add() {
		if (!empty($this->data)) {
			$this->Player->create();
			if ($this->Player->save($this->data)) {
				$this->Session->setFlash(__('The Player has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Player could not be saved. Please, try again.', true));
			}
		}
		$playerNames = $this->Player->PlayerName->find('list');
		$playerTypes = $this->Player->PlayerType->find('list');
		$teams = $this->Player->Team->find('list');
		$races = $this->Player->Race->find('list');
		$playerLevels = $this->Player->PlayerLevel->find('list');
		$this->set(compact('playerNames', 'playerTypes', 'teams', 'races', 'playerLevels'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Player', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Player->save($this->data)) {
				$this->Session->setFlash(__('The Player has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Player could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Player->read(null, $id);
		}
		$playerNames = $this->Player->PlayerName->find('list');
		$playerTypes = $this->Player->PlayerType->find('list');
		$teams = $this->Player->Team->find('list');
		$races = $this->Player->Race->find('list');
		$playerLevels = $this->Player->PlayerLevel->find('list');
		$this->set(compact('playerNames','playerTypes','teams','races','playerLevels'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Player', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Player->del($id)) {
			$this->Session->setFlash(__('Player deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>
